﻿
@{
    ViewBag.Title = "InfoView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>分包合同信息</legend>
</fieldset>
<div class="layui-tab">
    <ul class="layui-tab-title">
        <li class="layui-this">合同信息</li>
        <li>合同附件</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <form class="layui-form" enctype="multipart/form-data">
                <div class="layui-form-item">
                    <label class="layui-form-label">搜索</label>
                    <div class="layui-input-block">
                        <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入项目名或合同名称" class="layui-input" style="width:300px;float:left;">
                        <input type="text" value="" style="display:none" />
                        <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                        <div class="layui-form-mid layui-word-aux">高级搜索</div>
                        <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>

                        @*<button type="button" class="layui-btn layui-btn-normal" onclick="apply()"><i class="layui-icon">&#xe63b;</i></button>*@
                    </div>
                </div>
            </form>
            <table id="demo" lay-filter="SubConTableList"></table>
        </div>
        <div class="layui-tab-item">
            <form class="layui-form" enctype="multipart/form-data">
                <div class="layui-form-item">
                    <label class="layui-form-label">主项目选择</label>
                    <div class="layui-input-block">
                        <select name="ProjectId" lay-verify="required" lay-search lay-filter="FileSelFilter" id="UploadProjectId">
                            <option value=""></option>
                            @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                            {
                                <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">上传合同</label>
                    <label class="layui-form-label" style="width:300px;">（仅限以下格式：xls|xlsx|doc|docx|zip|rar）</label>
                    <div class="layui-input-block">
                        @*<input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="关键字" class="layui-input" style="width:300px;float:left;">
                            <input type="text" value="" style="display:none" />
                            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>*@
                        <button type="button" class="layui-btn layui-btn-danger" id="UpLoadExcel">
                            <i class="layui-icon">&#xe67c;</i>上传商品表格
                        </button>
                        <button type="button" style="display:none" id="UpLoadHideBtn"></button>

                    </div>
                </div>
            </form>
            <table id="demo2" lay-filter="FileTableList"></table>
        </div>
    </div>

</div>

<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>
</script>

<script type="text/javascript">
    var table;
    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var element = layui.element;
        var upload = layui.upload;
        var form = layui.form;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var index;

        $("#BtnAdd").on("click", function () {
            //$("#addCardFrom").css("display", "block");
            //index = layer.open({
            //    type: 2,
            //    area: ['700px', '200px'],
            //    content: '/Project/InfoAdd' //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            //});
            parent.layui.index.openTabsPage('/SubContract/SubConInfoAddView', '新增分包合同');
        });

        $("#BtnSave").on("click", function () {
            layer.close(index);
        });

        form.on('select(FileSelFilter)', function (data) {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            table.reload("demo2", {
                where: {
                    SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and TypeName='SubContract' and ProjectId=" + data.value
                }
            });
        });


        var uploadInst = upload.render({
            elem: '#UpLoadExcel' //绑定元素
            , url: '/SubContract/Upload/' //上传接口
            , accept: 'file'
            , size: 1024000
            , exts: "xls|xlsx|doc|docx|zip|rar"
            , data: {
                "ProjectId": function () {
                    return $("#UploadProjectId").val();
                }
            }
            , before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                LayerIndex = layer.open({
                    type: 1,
                    title: '进度条',
                    area: ['700px', '200'],
                    content: $("#LayerProgBar").html(),
                    closeBtn: 0

                });
                //然后需要重新渲染一下上传进度条
                element.init();
            }
            , progress: function (e, percent) {
                console.log("进度：" + percent + '%');
                element.progress('progressBar', percent + '%');
            }
            , done: function (msg, index, upload) {
                //上传完毕回调
                layer.close(LayerIndex);
                if (msg.result != "OK") {
                    layer.msg(msg.result, { icon: 6, time: 1000 });
                }
                else {
                    layer.msg('上传成功', { icon: 6, time: 1000 });
                    table.reload("demo2", {
                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                    });
                }

            }
            , error: function () {
                //请求异常回调
                alert(msg.result);
            }
        });

        ////监听单元格编辑
        //table.on('edit(UserTableList)', function (obj) {
        //    var value = obj.value //得到修改后的值
        //        , data = obj.data //得到所在行所有键值
        //        , field = obj.field; //得到字段
        //    layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
        //});

        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/SubContract/InfoTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "SubContractId", Order: "Desc", Condition: " and a.IsDelete<>1" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '项目名称', width: 250 }
                , { field: 'HTMC', title: '合同名称', width: 250 }
                , { field: 'HTLX', title: '合同类型', width: 100 }
                , { field: 'DFDWMC', title: '对方单位名称', width: 180 }
                , {
                    field: 'HTJE', title: '合同金额', width: 100,
                    templet: function (d) {
                        return dealNumber(d.ConAmount);
                    }
                }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">编辑</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        //鼠标双击事件回调
        table.on('rowDouble(SubConTableList)', function (obj) {
            var data = obj.data;
            index = layer.open({
                type: 2,
                area: ['700px', '500px'],
                content: '/SubContract/SubConBriefView?SubContractId=' + data.SubContractId, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            });
            //alert(data);
        });

        table.on("tool(SubConTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                parent.layui.index.openTabsPage('/SubContract/SubConInfoEditView?SubContractId=' + data.SubContractId, '分包合同编辑');

            }

            if (layEvent == 'TableDel') {
                layer.confirm('确实删除：' + data.Name + '->' + data.HTMC, { icon: 3, title: '提示' }, function (index) {
                    //do something
                    $.ajax({
                        type: "POST",
                        url: "/SubContract/SubConInfoDel",
                        async: false,
                        data: { "SubContractId": data.SubContractId },
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('删除成功', { icon: 6, time: 1000 });
                                table.reload("demo", {
                                    where: {
                                        SortName: "SubContractId", Order: "Desc", Condition: " and a.IsDelete<>1"
                                    }
                                });
                            } else {
                                alert(msg.result);
                            }
                        }
                    });
                    layer.close(index);
                });
            }

            if (layEvent == 'RestPW') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要重置该用户登录密码？', {
                    btn: ['确定重置', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/User/RestPW",
                            async: false,
                            data: { "UserId": data.UserId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('密码重置为"123456"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });

        table.render({
            elem: '#demo2'
            , method: 'post'
            //, height: 315
            , url: '/Purchase/FileTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "FileId", Order: "Desc", Condition: " and IsDelete<>1 and 1=0" }
            , limit: 20
            , limits: [20, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '文件名', width: 250 }
                , {
                    title: "下载", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Download">下载</a>';
                    }
                }
                , {
                    field: "operation", title: "操作", width: 180,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });

        table.on("tool(FileTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'Download') {
                $.down('/Purchase/Download', 'Path::' + data.Path + "&Name::" + data.Name, 'post');


            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.Name + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index) {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Purchase/FilelDel",
                            async: false,
                            data: { "FileId": data.FileId },
                            success: function (msg) {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo2", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index) {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });


    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "SubContractId", Order: "Desc", Condition: " and a.IsDelete<>1"
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "SubContractId", Order: "Desc", Condition: " and a.IsDelete<>1 and (Name like '%" + text + "%' or HTMC like '%" + text + "%')"
                },
                page: {
                    curr: 1
                }
            });
        }
    }



    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };




</script>

